package com.cy.server;

import com.cy.container.Mapper;
import com.cy.container.Wrapper;
import com.cy.loader.MyClassLoader;
import com.cy.servlet.HttpServlet;

import java.net.Socket;
import java.util.Map;

/**
 * @Author ChenYan
 * @Date 2021/8/13 14:55
 * @Description
 */
public class RequestProcessor extends Thread{

    private Socket socket;
    private Mapper mapper;

    public RequestProcessor(Socket socket , Mapper mapper){
        this.socket = socket;
        this.mapper = mapper;
    }

    @Override
    public void run() {
        try {
            //封装Request和Response
            Request request = new Request(socket.getInputStream());
            Response response = new Response(socket.getOutputStream());

            Wrapper wrapper = mapper.findWrapperByUrl(request.getUrl());
            if (wrapper == null){
                response.outputHtml(request.getUrl());
            }
            else {
                wrapper.invokeService(request, response);
            }

            //没有servlet表示是静态资源
//            if (servletMap.get(request.getUrl()) == null){
//                response.outputHtml(request.getUrl());
//            }
//            else {
//                //动态资源请求
//                HttpServlet httpServlet = servletMap.get(request.getUrl());
//                httpServlet.service(request, response);
//            }

            socket.close();
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }
}
